﻿using System.Collections.Generic;

namespace fastJSON
{
    internal class SafeDictionary<TKey, TValue>
    {
        private readonly Dictionary<TKey, TValue> _Dictionary = new Dictionary<TKey, TValue>();
        private readonly object _Padlock = new object();

        public TValue this[TKey key] { get { return this._Dictionary[key]; } }
        public bool TryGetValue(TKey key, out TValue value)
        {
            return this._Dictionary.TryGetValue(key, out value);
        }
        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            return ((ICollection<KeyValuePair<TKey, TValue>>) this._Dictionary).GetEnumerator();
        }

        public void Add(TKey key, TValue value)
        {
            lock (this._Padlock)
            {
                if (this._Dictionary.ContainsKey(key) == false)
                {
                    this._Dictionary.Add(key, value);
                }
            }
        }
    }
}